﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DotSpatial.Topology;

namespace PoemsSystem
{
    public partial class FilterForm : Form
    {
        public FilterForm()
        {
            InitializeComponent();
        }

        private void btnSelectByPoemName_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtPoemName.Text))
            {
                MessageBox.Show("请输入一个诗名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPoemName.Text = "";
            }
            else
            {
                SqlDataReader reader1 = Database.ReadData(txtPoemName.Text);
                if (reader1.Read()==false)
                {
                    MessageBox.Show("该诗人未写过该诗", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                reader1.Close();
                SqlDataReader reader = Database.ReadData(txtPoemName.Text);
                rtxtInfo.SelectionFont = new Font("华文行楷", 12, FontStyle.Bold);
                String content1 = "";
                if (reader.Read())
                {
                    String content= (String)reader[1];
                    content1 += "\n" + (String)reader[0] + "\n";
                    for (int i=0;i<content.Length;i++)
                    {
                        if (content[i]!='|')
                        {
                            content1 += content[i];
                        }
                        if (content[i]=='。')
                        {
                            content1 += "\n";
                        }
                    }
                    
                }
                rtxtInfo.SelectAll();
                rtxtInfo.SelectionAlignment = HorizontalAlignment.Center;
                rtxtInfo.Text =  content1;
                MainForm.location = new Coordinate();
                MainForm.location.X = double.Parse(reader[4].ToString());
                MainForm.location.Y = double.Parse(reader[5].ToString());
                MainForm.IfDrawLocation = true;
                reader.Close();
            }
        }

        private void FilterForm_Load(object sender, EventArgs e)
        {

        }

        private void btnSelectbyloc_Click_1(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtLocName.Text))
            {
                MessageBox.Show("请输入一个地名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtLocName.Text = "";
            }
            else if (ckbAncLoc.Checked==false)
            {
                SqlDataReader reader1 = Database.Readloc(txtLocName.Text);
                if (reader1.Read() == false)
                {
                    MessageBox.Show("该诗人从未到过此地", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                reader1.Close();
                SqlDataReader reader = Database.Readloc(txtLocName.Text);
                rtxtInfo.SelectionFont = new Font("华文行楷", 12, FontStyle.Bold);
                String content1 = "";
                int j = 0;
                while (reader.Read())
                {
                    j++;
                    if (j==1)
                    {
                        MainForm.location = new Coordinate();
                        MainForm.location.X = double.Parse(reader[4].ToString());
                        MainForm.location.Y = double.Parse(reader[5].ToString());
                        MainForm.IfDrawLocation = true;
                    }
                    String content = (String)reader[1];
                    content1 += "\n" + (String)reader[0] + "\n";
                    for (int i = 0; i < content.Length; i++)
                    {
                        if (content[i] != '|')
                        {
                            content1 += content[i];
                        }
                        if (content[i] == '。')
                        {
                            content1 += "\n";
                        }
                    }
                }
                rtxtInfo.SelectAll();
                rtxtInfo.SelectionAlignment = HorizontalAlignment.Center;
                rtxtInfo.Text = content1;
                reader.Close();
            }
            else if (ckbAncLoc.Checked == true)
            {
                SqlDataReader reader1 = Database.Readancloc(txtLocName.Text);
                if (reader1.Read() == false)
                {
                    MessageBox.Show("该诗人从未到过此地", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                reader1.Close();
                SqlDataReader reader = Database.Readancloc(txtLocName.Text);
                rtxtInfo.SelectionFont = new Font("华文行楷", 12, FontStyle.Bold);
                String content1 = "";
                int j = 0;
                while (reader.Read())
                {
                    j++;
                    if (j==1)
                    {
                        MainForm.location = new Coordinate();
                        MainForm.location.X = double.Parse(reader[4].ToString());
                        MainForm.location.Y = double.Parse(reader[5].ToString());
                        MainForm.IfDrawLocation = true;
                    }
                    String content = (String)reader[1];
                    content1 += "\n" + (String)reader[0] + "\n";
                    for (int i = 0; i < content.Length; i++)
                    {
                        if (content[i] != '|')
                        {
                            content1 += content[i];
                        }
                        if (content[i] == '。')
                        {
                            content1 += "\n";
                        }
                    }   
                }
                rtxtInfo.SelectAll();
                rtxtInfo.SelectionAlignment = HorizontalAlignment.Center;
                rtxtInfo.Text = content1;
                MainForm.location = new Coordinate();
                MainForm.location.X = double.Parse(reader[4].ToString());
                MainForm.location.Y = double.Parse(reader[5].ToString());
                MainForm.IfDrawLocation = true;
                reader.Close();
            }
        }

        private void btnSelectbytime_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtTime1.Text)&&String.IsNullOrEmpty(txtTime2.Text))
            {
                MessageBox.Show("请输入时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTime1.Text = "";
            }
            else if (String.IsNullOrEmpty(txtTime2.Text))
            {
                SqlDataReader reader1 = Database.Readtime1(Convert.ToSingle(txtTime1.Text));
                if (reader1.Read() == false)
                {
                    MessageBox.Show("请输入正确的时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                reader1.Close();
                SqlDataReader reader = Database.Readtime1(Convert.ToSingle(txtTime1.Text));
                rtxtInfo.SelectionFont = new Font("华文行楷", 12, FontStyle.Bold);
                String content1 = "";
                float time = 0;
                while (reader.Read())
                {
                    if (Convert.ToSingle(reader[6]) != time)
                    {
                        time = Convert.ToSingle(reader[6]);
                        content1 += "\n" + time + "年\n";
                    }
                    String content = (String)reader[1];
                    content1 += "\n" + (String)reader[0] + "\n";
                    for (int i = 0; i < content.Length; i++)
                    {
                        if (content[i] != '|')
                        {
                            content1 += content[i];
                        }
                        if (content[i] == '。')
                        {
                            content1 += "\n";
                        }
                    }
                }
                rtxtInfo.SelectAll();
                rtxtInfo.SelectionAlignment = HorizontalAlignment.Center;
                rtxtInfo.Text = content1;
                reader.Close();
            }
            else if (String.IsNullOrEmpty(txtTime1.Text))
            {
                SqlDataReader reader1 = Database.Readtime2(Convert.ToSingle(txtTime2.Text));
                if (reader1.Read() == false)
                {
                    MessageBox.Show("请输入正确的时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                reader1.Close();
                SqlDataReader reader = Database.Readtime2(Convert.ToSingle(txtTime2.Text));
                rtxtInfo.SelectionFont = new Font("华文行楷", 12, FontStyle.Bold);
                String content1 = "";
                float time = 0;
                while (reader.Read())
                {
                    if (Convert.ToSingle(reader[6]) != time)
                    {
                        time = Convert.ToSingle(reader[6]);
                        content1 += "\n" + time + "年\n";
                    }
                    String content = (String)reader[1];
                    content1 += "\n" + (String)reader[0] + "\n";
                    for (int i = 0; i < content.Length; i++)
                    {
                        if (content[i] != '|')
                        {
                            content1 += content[i];
                        }
                        if (content[i] == '。')
                        {
                            content1 += "\n";
                        }
                    }
                }
                rtxtInfo.SelectAll();
                rtxtInfo.SelectionAlignment = HorizontalAlignment.Center;
                rtxtInfo.Text = content1;
                reader.Close();
            }
            else
            {
                SqlDataReader reader1 = Database.Readtime(Convert.ToSingle(txtTime1.Text), Convert.ToSingle(txtTime2.Text));
                if (reader1.Read() == false)
                {
                    MessageBox.Show("请输入正确的时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                reader1.Close();
                SqlDataReader reader = Database.Readtime(Convert.ToSingle(txtTime1.Text), Convert.ToSingle(txtTime2.Text));
                rtxtInfo.SelectionFont = new Font("华文行楷", 12, FontStyle.Bold);
                String content1 = "";
                float time = 0;
                while (reader.Read())
                {
                    if (Convert.ToSingle(reader[6]) != time)
                    {
                        time = Convert.ToSingle(reader[6]);
                        content1 += "\n" + time + "年\n";
                    }
                    String content = (String)reader[1];
                    content1 += "\n" + (String)reader[0] + "\n";
                    for (int i = 0; i < content.Length; i++)
                    {
                        if (content[i] != '|')
                        {
                            content1 += content[i];
                        }
                        if (content[i] == '。')
                        {
                            content1 += "\n";
                        }
                    }
                }
                rtxtInfo.SelectAll();
                rtxtInfo.SelectionAlignment = HorizontalAlignment.Center;
                rtxtInfo.Text = content1;
                reader.Close();
            }
        }
    }
}
